﻿Public Class ZeitrapportServiceImpl
    Inherits PersistenceServiceImpl(Of Zeitrapport, ZeitrapportManager)
    Implements ZeitrapportService

    Sub New()
        MyBase.New(New ZeitrapportManagerImpl())
    End Sub

    Protected Overrides Sub beforeDelete(ByRef entity As Zeitrapport)
        ' do nothing
    End Sub

    Protected Overrides Sub beforeSave(ByRef entity As Zeitrapport)
        ' do nothing
    End Sub

    Protected Overrides Sub fillUp(ByRef entity As Zeitrapport)
        ' take the mitarbeiter
        entity.mitarbeiter = Context.MITARBEITER_SERVICE.findById(entity.mitarbeiter)
    End Sub

    Public Sub deleteByMitarbeiter(ByRef mitarbeiter As Mitarbeiter) Implements ZeitrapportService.deleteByMitarbeiter
        addFireBlock()
        Connection.open()
        Dim zeitrapporte As List(Of Zeitrapport) = manager.findByMitarbeiter(mitarbeiter)
        For Each zeitrapport As Zeitrapport In zeitrapporte
            beforeDelete(zeitrapport)
        Next
        manager.deleteByMitarbeiter(mitarbeiter)
        Connection.close()
        removeFireBlock()
        For Each zeitrapport As Zeitrapport In zeitrapporte
            fireDeleted(zeitrapport)
        Next
    End Sub

    Public Sub deleteByTask(ByRef task As Task) Implements ZeitrapportService.deleteByTask
        addFireBlock()
        Connection.open()
        For Each zeitrapport As Zeitrapport In task.zeitrapporte
            beforeDelete(zeitrapport)
        Next
        manager.deleteByTask(task)
        Connection.close()
        removeFireBlock()
        For Each zeitrapport As Zeitrapport In task.zeitrapporte
            fireDeleted(zeitrapport)
        Next
    End Sub

    Public Function findByTask(ByRef task As Task) As System.Collections.Generic.List(Of Zeitrapport) Implements ZeitrapportService.findByTask
        Connection.open()
        Dim zeitrapporte As List(Of Zeitrapport) = manager.findByTask(task)
        For Each zeitrapport As Zeitrapport In zeitrapporte
            fillUp(zeitrapport)
        Next
        Connection.close()
        Return zeitrapporte
    End Function
End Class
